package com.grad.why.studentsystem;

import com.grad.why.studentsystem.domain.Camera;
import com.grad.why.studentsystem.domain.ClassRoom;
import com.grad.why.studentsystem.domain.MyTask;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinfo;
import com.grad.why.studentsystem.domain.current.ClassRoomIdAndCourseArrangeId;
import com.grad.why.studentsystem.domain.current.CurrentCourseArrangeInfo;
import com.grad.why.studentsystem.mapper.KillinfosMapper;
import com.grad.why.studentsystem.mapper.RuntimecoursearrangeinfoMapper;
import com.grad.why.studentsystem.service.KillInfosService;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.RuntimecoursearrangeinfoService;
import com.grad.why.studentsystem.service.impl.CustomMultiThreadingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by 谭健 on 2018/4/10 0010. 星期二. 15:48.
 * © All Rights Reserved.
 * <p>
 * ApplicationRunner
 * 该接口的方法会在服务启动之后被立即执行
 * 主要用来做一些初始化的工作
 * 但是该方法的运行是在SpringApplication.run(…​) 执行完毕之前执行
 */

@Component
@Slf4j
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Autowired
    private CustomMultiThreadingService customMultiThreadingService;

    @Autowired
    private OptionparamsService optionparamsService;

    @Autowired
    private RuntimecoursearrangeinfoService runtimecoursearrangeinfoService;

    @Autowired
    private KillInfosService killInfosService;


    /**
     * 会在服务启动完成后立即执行
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //init();
    }

    public void init() throws Exception {
        killInfosService.initDropDatas();
        Thread.sleep(1000);
        customMultiThreadingService.initCourseInfos();
        customMultiThreadingService.getRunningCourseAndCourseArrangeInfos();
        Thread.sleep(2000);
        File file = new File("D:\\git\\face_recognition-master\\face_recognition-master\\mytest");
        if (!file.exists()) {
            file.mkdirs();
        }
        File capVideoFile = new File("D:\\git\\face_recognition-master\\face_recognition-master\\mytest\\cvCaptureDemo.py");
        while(true) {
            //需替换成按摄像头来进行处理，从而得到多教室多角度的人脸识别
            List<Runtimecoursearrangeinfo> runtimecoursearrangeinfos = checkDataSetExist();
            if(runtimecoursearrangeinfos != null) {
                List<ClassRoomIdAndCourseArrangeId> classRoomIdAndCourseArrangeIds = new ArrayList<>();
                for (int i = 0; i < runtimecoursearrangeinfos.size(); i++) {
                    Runtimecoursearrangeinfo runtimecoursearrangeinfo = runtimecoursearrangeinfos.get(i);
                    List<Camera> cameras = runtimecoursearrangeinfoService.getCamerasByClassRoomId(runtimecoursearrangeinfo.getClassroomid());
                    for (int j = 0; j < cameras.size(); j++) {
                        Camera camera = cameras.get(j);
                        if(camera.getUsetype()==1){
                            File distributeCapture = new File("D:\\git\\face_recognition-master\\face_recognition-master\\mytest\\cvCaptureDemo" + camera.getCameraid() + ".py");
                            if (!distributeCapture.exists()) {
                                copyFileUsingApacheCommonsIO(capVideoFile, distributeCapture);
                            }
                            int n = killInfosService.checkCameraIsRunning(camera.getCameraid());
                            if(n == 0){
                                killInfosService.insertRunningCameraInfo(camera.getCameraid());
                                customMultiThreadingService.accordingClassRoomIdGetCameraId(runtimecoursearrangeinfos.get(i).getCoursearrangeid(), camera);
                            }
                        }else if(camera.getUsetype() == 2){
                            File checkModel = new File("D:\\git\\FCHD-Fully-Convolutional-Head-Detector\\checkpoints\\head_detector_final" + camera.getCameraid());
                            if (!checkModel.exists()) {
                                copyFileUsingApacheCommonsIO(capVideoFile, checkModel);
                            }
                            int n = killInfosService.checkCameraIsRunning(camera.getCameraid());
                            if(n == 0) {
                                killInfosService.insertRunningCameraInfo(camera.getCameraid());
                            }
                        }
                    }
                }
            }else{
                System.out.println("当前没有正在进行的课程....");
            }
            Thread.sleep(1000 * 30);
        }
    }

    private static void copyFileUsingApacheCommonsIO(File source, File dest)
            throws IOException {
        FileUtils.copyFile(source, dest);
    }

    public List<Runtimecoursearrangeinfo> checkDataSetExist() {
        int count = 0;
        int steps = 0;
        List<Runtimecoursearrangeinfo> runtimecoursearrangeinfos = null;
        while (true) {
            count = Integer.parseInt(optionparamsService.getParamValueByParamName("runtimecount"));
            if (count > 0) {
                break;
            }
            steps++;
            if (steps > 10) {
                break;
            }
        }
        if (count > 0) {
            runtimecoursearrangeinfos = runtimecoursearrangeinfoService.getAllRuntimeCourseInfos();
            while (runtimecoursearrangeinfos.size() != count) {
                runtimecoursearrangeinfos = runtimecoursearrangeinfoService.getAllRuntimeCourseInfos();
            }
        }
        if (count > 0) {
            return runtimecoursearrangeinfos;
        } else {
            return null;
        }
    }
}



